package top.yokey.shopnc.activity.main;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutCompat;
import android.view.KeyEvent;
import android.view.View;

import com.squareup.otto.Subscribe;

import top.yokey.base.util.TextUtil;
import top.yokey.shopnc.R;
import top.yokey.shopnc.adapter.BaseFragmentAdapter;
import top.yokey.shopnc.base.BaseActivity;
import top.yokey.shopnc.base.BaseApplication;
import top.yokey.shopnc.base.BaseConstant;
import top.yokey.base.base.BaseSnackBar;
import top.yokey.shopnc.event.MainPositionEvent;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

@ContentView(R.layout.activity_main_main)
public class MainActivity extends BaseActivity {

    @ViewInject(R.id.mainLinearLayout)
    private LinearLayoutCompat mainLinearLayout;
    @ViewInject(R.id.mainViewPager)
    private ViewPager mainViewPager;
    @ViewInject(R.id.homeTextView)
    private AppCompatTextView homeTextView;
    @ViewInject(R.id.cateTextView)
    private AppCompatTextView cateTextView;
    @ViewInject(R.id.searchTextView)
    private AppCompatTextView searchTextView;
    @ViewInject(R.id.cartTextView)
    private AppCompatTextView cartTextView;
    @ViewInject(R.id.mineTextView)
    private AppCompatTextView mineTextView;

    private long exitTimeLong;
    private Drawable[] navigationPressDrawable;
    private Drawable[] navigationNormalDrawable;
    private AppCompatTextView[] navigationTextView;

    @Override
    public void initData() {

        exitTimeLong = 0L;

        navigationTextView = new AppCompatTextView[5];
        navigationTextView[0] = homeTextView;
        navigationTextView[1] = cateTextView;
        navigationTextView[2] = searchTextView;
        navigationTextView[3] = cartTextView;
        navigationTextView[4] = mineTextView;

        navigationNormalDrawable = new Drawable[navigationTextView.length];
        navigationNormalDrawable[0] = BaseApplication.get().getMipmap(R.drawable.ic_navigation_home);
        navigationNormalDrawable[1] = BaseApplication.get().getMipmap(R.drawable.ic_navigation_cate);
        navigationNormalDrawable[2] = BaseApplication.get().getMipmap(R.drawable.ic_navigation_search);
        navigationNormalDrawable[3] = BaseApplication.get().getMipmap(R.drawable.ic_navigation_cart);
        navigationNormalDrawable[4] = BaseApplication.get().getMipmap(R.drawable.ic_navigation_mine);

        navigationPressDrawable = new Drawable[navigationTextView.length];
        navigationPressDrawable[0] = BaseApplication.get().getMipmap(R.drawable.ic_navigation_home_press);
        navigationPressDrawable[1] = BaseApplication.get().getMipmap(R.drawable.ic_navigation_cate_press);
        navigationPressDrawable[2] = BaseApplication.get().getMipmap(R.drawable.ic_navigation_search_press);
        navigationPressDrawable[3] = BaseApplication.get().getMipmap(R.drawable.ic_navigation_cart_press);
        navigationPressDrawable[4] = BaseApplication.get().getMipmap(R.drawable.ic_navigation_mine_press);

        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new HomeFragment());
        fragmentList.add(new CateFragment());
        fragmentList.add(new SearchFragment());
        fragmentList.add(new CartFragment());
        fragmentList.add(new MineFragment());

        mainViewPager.setAdapter(new BaseFragmentAdapter(getSupportFragmentManager(), fragmentList));
        mainViewPager.setOffscreenPageLimit(navigationTextView.length);

        updateNavigation(0);

    }

    @Override
    public void initEven() {

        mainViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                updateNavigation(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        for (int i = 0; i < navigationTextView.length; i++) {
            final int position = i;
            navigationTextView[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    updateNavigation(position);
                }
            });
        }

    }

    @Override
    public void onReturn() {

        if (System.currentTimeMillis() - exitTimeLong > BaseConstant.TIME_EXIT) {
            BaseSnackBar.get().show(mainLinearLayout, "再按一次返回桌面...");
            exitTimeLong = System.currentTimeMillis();
        } else {
            BaseApplication.get().startHome(getActivity());
        }

    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
            onReturn();
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    public void onActivityResult(int req, int res, Intent intent) {
        super.onActivityResult(req, res, intent);
        if (res == RESULT_OK && req == BaseConstant.CODE_QRCODE) {
            handlerQRCode(intent.getStringExtra("result"));
        }
    }

    //自定义方法

    private void handlerQRCode(String result) {

        if (result.contains(BaseConstant.URL)) {
            return;
        }

        if (TextUtil.isUrl(result)) {
            BaseApplication.get().startBrowser(getActivity(), result);
            return;
        }

    }

    private void updateNavigation(int position) {

        for (int i = 0; i < navigationTextView.length; i++) {
            navigationTextView[i].setTextColor(BaseApplication.get().getColors(R.color.grey));
            navigationTextView[i].setCompoundDrawablesWithIntrinsicBounds(null, navigationNormalDrawable[i], null, null);
        }

        navigationTextView[position].setTextColor(BaseApplication.get().getColors(R.color.primary));
        navigationTextView[position].setCompoundDrawablesWithIntrinsicBounds(null, navigationPressDrawable[position], null, null);
        mainViewPager.setCurrentItem(position);

    }

    @Subscribe
    @SuppressWarnings("unused")
    public void onMainSearchEvent(MainPositionEvent event) {

        mainViewPager.setCurrentItem(event.getPosition());

    }

}
